﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GlobalHelper;
using IBSGate;
using Loader;
using DBGate=OPMSIBSDBGate.DBGate;

namespace Viwer
{
    public partial class BSLoader : Form
    {
        private string _mainId;

        public BSLoader()
        {
            InitializeComponent();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            string path = string.Empty;

            OpenFileDialog file_open = new OpenFileDialog();
            file_open.Filter = "EXCEL文件(*.xls)|*.xls";
            if (file_open.ShowDialog() == DialogResult.OK)
            {
                path = file_open.FileName;
            }

            string sheetName = tbxSheetName.Text;

            IBSExcelOperator excelOperator = new IBSExcelOperator();

            DataTable dt = excelOperator.GetDataTableFromExcel(path, sheetName);
            dataGridView1.DataSource = dt;

            Loader.CreateTimeTable creator = new CreateTimeTable(dt);
            string mainInfoId = creator.CreateMainInfoWithoutLineIdWithSationName();

            BusCompany busCompanyFlag = BusCompany.One;
            Guid mainId = new Guid(mainInfoId);
            creator.CreateDetailInfo(mainId,busCompanyFlag);

            MessageBox.Show(mainInfoId);

            _mainId = mainInfoId;
        }

        private void btnReflush_Click(object sender, EventArgs e)
        {
            //if (string.IsNullOrEmpty(_mainId))
            //{
            //    return;
            //}

            //DBGate dbGate = new DBGate();
            //DataTable dt = dbGate.GetDetailInfo(Convert.ToInt32(_mainId));

            //dataGridView1.DataSource = dt;
        }

        private void btnBatch_Click(object sender, EventArgs e)
        {
            string mainInfoId = string.Empty;

            if (!Helper.CheckIsNumber(tbxSheetAmount.Text))
            {
                MessageBox.Show("请输入数字!");
                tbxSheetAmount.Text = string.Empty;
                return;
            }

            try
            {
                BusCompany busCompanyFlag = GetBusCompanyFlag();

                //init pgb
                pgbLoad.Maximum = Convert.ToInt32(tbxSheetAmount.Text);
                pgbLoad.Step = 1;

                //do it
                string path = string.Empty;

                OpenFileDialog file_open = new OpenFileDialog();
                file_open.Filter = "EXCEL文件(*.xls)|*.xls";
                if (file_open.ShowDialog() == DialogResult.OK)
                {
                    path = file_open.FileName;


                    int sheetAmount = Convert.ToInt32(tbxSheetAmount.Text);

                    IBSExcelOperator excelOperator = new IBSExcelOperator();


                    DataTable dt = new DataTable();

                    for (int i = 1; i <= sheetAmount; i++)
                    {
                        try
                        {
                            dt = excelOperator.GetDataTableFromExcel(path, i.ToString());

                            Loader.CreateTimeTable creator = new CreateTimeTable(dt);
                            mainInfoId = creator.CreateMainInfoWithoutLineIdWithSationName();

                            //BusCompany busCompanyFlag = BusCompany.One;
                            Guid mainId = new Guid(mainInfoId);
                            creator.CreateDetailInfo(mainId,busCompanyFlag);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(string.Format(@"导入失败,在 {0} Sheet处, 出错原因为:{1},
                                                        具体信息为: {2} "
                                , i, ex.Message, ex.StackTrace));
                        }

                        pgbLoad.PerformStep();

                        lblInfo.Text = string.Format("表{0}导入成功!", i);

                    }

                    MessageBox.Show("导入成功!");
                    lblInfo.Text = "准备导入.";
                    pgbLoad.Value = 0;

                    Guid mainGuid = new Guid(mainInfoId);
                    dataGridView1.DataSource = new DBGate().GetDetailInfo(mainGuid);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            

        }

        private BusCompany GetBusCompanyFlag()
        {
            BusCompany reslut = new BusCompany();

            if (rdbCompanyOne.Checked)
            {
                reslut = BusCompany.One;
            }
            else if (rdbCompanyFive.Checked)
            {
                reslut = BusCompany.Five;
            }

            return reslut;
        }

        private void BSLoader_Load(object sender, EventArgs e)
        {
            lblInfo.Text = "准备导入.";

        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbxLineId.Text)
                || !Helper.CheckIsNumber(tbxLineId.Text))
            {
                MessageBox.Show("输入的确线路ID格式不正确!");
            }

            DBGate dbGate = new DBGate();
            if (tbxLineId.Text != null)
            {
                Guid mainGuid = new Guid(tbxLineId.Text);
                DataTable dt = dbGate.GetDetailInfo(mainGuid);

                dataGridView1.DataSource = dt;
            }
        }





        #region Not Use

        //private bool _inited = false;
        //private bool _depInited = false;

        //private void BSLoader_Load(object sender, EventArgs e)
        //{
        //    //IList<ITSSYSModel.y_Department> companys = new ITSSYSBLL.y_Department().GetInfoByParentId(Helper.RootCompanyId);
        //    //cbxCompany.DataSource = companys;
        //    //cbxCompany.DisplayMember = Helper.DepartmentDisplayMember;
        //    //cbxCompany.ValueMember = Helper.DepartmentValueMember;

        //    Dictionary<string,string> dic = new Dictionary<string, string>();
        //    dic.Add("A", "1");
        //    dic.Add("B", "2");

        //    BindingSource bs = new BindingSource();
        //    bs.DataSource = dic;

        //    cbxCompany.DataSource = bs;
        //    cbxCompany.DisplayMember = "Key";
        //    cbxCompany.ValueMember = "Value";


        //    _inited = true;
        //    _depInited = false;
        //}


        //private void cbxCompany_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (_inited)
        //    {
        //        MessageBox.Show(cbxCompany.SelectedValue.ToString());

        //        //string companyId = cbxCompany.SelectedValue.ToString();

        //        //IList<ITSSYSModel.y_Department> companys = new ITSSYSBLL.y_Department().GetInfoByParentId(Convert.ToInt32(companyId));
        //        //cbxDepartment.DataSource = companys;
        //        //cbxDepartment.DisplayMember = Helper.DepartmentDisplayMember;
        //        //cbxDepartment.ValueMember = Helper.DepartmentValueMember;

        //        //_depInited = true;
        //    }

        //}

        //private void cbxDepartment_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (_depInited)
        //    {
        //        string departmentId = cbxDepartment.SelectedValue.ToString();

        //        IList<ITSSYSModel.y_BusLine> companys = new ITSSYSBLL.y_BusLine().GetInfoByParentId(Convert.ToInt32(departmentId));
        //        cbxLine.DataSource = companys;
        //        cbxLine.DisplayMember = Helper.LineDisplayMember;
        //        cbxLine.ValueMember = Helper.LineValueMember;
        //    }
        //}

        #endregion
    }
}
